package com.example.class_manage.Config;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


@Configuration
@EnableSwagger2
public class SwaggerConfig{


    @Bean
    public Docket api(){
        List<Parameter> pars = new ArrayList<Parameter>();
        ParameterBuilder t = new ParameterBuilder();
        //配置h头信息
        t.name("t").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(t.build());
        return new Docket(DocumentationType.SWAGGER_2)
                // 选择那些路径和api会生成document
                .select()
                //对所有api进行监控
                .apis(RequestHandlerSelectors.any())
                //配置swagger扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.class_manage.Controller"))
                //错误路径不监控
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                //actuator路径不监控
                .paths(Predicates.not(PathSelectors.regex("/actuator.*")))
                .build()
                .globalOperationParameters(pars)
                .apiInfo(apiInfo());
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("Class——manage")
                //描述
                .description("服务")
                //服务条款 URL
                .termsOfServiceUrl("http://www.baidu.com")
                //版本号
                .version("1.0")
                .build();
    }



}

